<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注销功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        h1 {
            color: #333;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        button {
            background-color: #f44336;
            color: white;
            border: none;
            padding: 10px 15px;
            margin: 10px 0;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #d32f2f;
        }
        pre {
            background-color: #f5f5f5;
            padding: 10px;
            border-radius: 4px;
            overflow: auto;
        }
        .info {
            background-color: #e3f2fd;
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <h1>注销功能测试</h1>
    
    <div class="info">
        <p>当前页面用于测试注销功能。这个页面可以帮助你直接测试各种注销场景，不需要依赖UI界面。</p>
    </div>
    
    <h2>1. 查看当前Cookie</h2>
    <button onclick="showCookies()">显示所有Cookie</button>
    <pre id="cookies-display"></pre>
    
    <h2>2. 测试常规注销</h2>
    <button onclick="testNormalLogout()">测试普通注销</button>
    <pre id="normal-logout-result"></pre>
    
    <h2>3. 测试所有设备注销</h2>
    <button onclick="testLogoutAllDevices()">测试所有设备注销</button>
    <pre id="all-devices-logout-result"></pre>
    
    <h2>4. 手动清除所有Cookie</h2>
    <button onclick="clearAllCookies()">清除所有Cookie</button>
    <pre id="clear-cookies-result"></pre>
    
    <script>
        // 显示所有Cookie
        function showCookies() {
            const display = document.getElementById('cookies-display');
            const cookies = document.cookie.split(';').map(cookie => cookie.trim());
            
            if (cookies.length === 0 || (cookies.length === 1 && cookies[0] === '')) {
                display.textContent = '没有找到Cookie';
            } else {
                display.textContent = '当前Cookie:\n' + cookies.join('\n');
            }
        }
        
        // 测试普通注销
        async function testNormalLogout() {
            const resultDisplay = document.getElementById('normal-logout-result');
            resultDisplay.textContent = '请求中...';
            
            try {
                // 使用fetch调用内置的NextAuth注销API
                const response = await fetch('/api/auth/signout', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ redirect: false }),
                });
                
                const data = await response.json();
                resultDisplay.textContent = 'API响应:\n' + JSON.stringify(data, null, 2);
                
                // 显示当前Cookie以验证是否已清除
                setTimeout(showCookies, 500);
            } catch (error) {
                resultDisplay.textContent = '错误:\n' + error.message;
            }
        }
        
        // 测试所有设备注销
        async function testLogoutAllDevices() {
            const resultDisplay = document.getElementById('all-devices-logout-result');
            resultDisplay.textContent = '请求中...';
            
            try {
                // 生成唯一请求ID
                const requestId = Date.now().toString();
                
                // 使用fetch调用自定义的注销所有设备API
                const response = await fetch(`/api/auth/logout-all?requestId=${requestId}`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Cache-Control': 'no-cache, no-store, must-revalidate',
                        'Pragma': 'no-cache',
                    },
                    cache: 'no-store',
                });
                
                const status = response.status;
                let data;
                
                try {
                    data = await response.json();
                } catch (e) {
                    data = { error: '无法解析响应' };
                }
                
                resultDisplay.textContent = `HTTP状态: ${status}\nAPI响应:\n` + JSON.stringify(data, null, 2);
                
                // 显示当前Cookie以验证是否已清除
                setTimeout(showCookies, 500);
            } catch (error) {
                resultDisplay.textContent = '错误:\n' + error.message;
            }
        }
        
        // 手动清除所有Cookie
        function clearAllCookies() {
            const resultDisplay = document.getElementById('clear-cookies-result');
            try {
                const cookies = document.cookie.split(';');
                let count = 0;
                
                cookies.forEach(cookie => {
                    const cookieName = cookie.split('=')[0].trim();
                    if (cookieName) {
                        document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/;`;
                        count++;
                    }
                });
                
                resultDisplay.textContent = `已尝试清除 ${count} 个Cookie`;
                
                // 显示当前Cookie以验证是否已清除
                setTimeout(showCookies, 500);
            } catch (error) {
                resultDisplay.textContent = '错误:\n' + error.message;
            }
        }
        
        // 页面加载时显示当前Cookie
        window.onload = showCookies;
    </script>
</body>
</html> 